package com.taoym.voucher.common.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @coder taoym
 * @datetime 2021/7/31 17:23
 * @desc threadLocal工具类
 */
public class ThreadLocalUtils {

    private static final ThreadLocal<UserInfo> THREAD_LOCAL = new ThreadLocal<>();

    //私有化构造
    private ThreadLocalUtils() {
    }

    public static void bind(String uid) {
        UserInfo userInfo = new UserInfo(uid);
        bind(userInfo);
    }

    public static void bind(UserInfo userInfo) {
        THREAD_LOCAL.set(userInfo);
    }

    public static UserInfo get() {
        return THREAD_LOCAL.get();
    }

    /**
     * 移除绑定的用户对象
     */
    public static void remove() {
        THREAD_LOCAL.remove();
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UserInfo {
        /**
         * 用户uid
         */
        private String uid;
    }
}
